/* eslint-disable no-undef */
/** EasyWeb spa v3.1.5 date:2019-10-05 License By http://easyweb.vip */
layui.define(['layer', 'element', 'config', 'layRouter', 'admin', 'contextMenu'], function(t) {
  var $ = layui.jquery;
  var layer = layui.layer;
  var element = layui.element;
  var config = layui.config;
  var layRouter = layui.layRouter;
  var admin = layui.admin;
  var contextMenu = layui.contextMenu;
  var $header = '.layui-layout-admin>.layui-header';
  var $sideScroll = '.layui-layout-admin>.layui-side>.layui-side-scroll';
  var $body = '.layui-layout-admin>.layui-body';
  var $tab = $body + '>.layui-tab';
  var $bodyHeader = $body + '>.layui-body-header';
  var PAGE_TABS = 'admin-pagetabs';
  var SIDE_NAV = 'admin-side-nav';
  var NPPP;
  var Cache = {
    mTabPosition: undefined,
    mTabList    : [],
    regRouter   : function(v) {
      $.each(v, function(x, y) {
        if (y.url && y.url.indexOf('#') == 0) {
          var w = Cache.getHashPath(y.url);
          layRouter.reg(w, function(z) {
            Cache.loadView({
              menuId  : z.href,
              menuPath: config.viewPath + w + Cache.getViewSuffix(w),
              menuName: y.name
            });
          });
        }
        if (y.subMenus) {
          Cache.regRouter(y.subMenus);
        }
      });
    },
    loadView: function(x) {
      var B = x.menuId;
      var v = x.menuPath;
      var w = x.menuName;
      var D = Cache.getHashPath('#' + B);
      var z = $body + '>div[lay-id]';
      if (config.pageTabs) {
        var E;
        $($tab + '>.layui-tab-title>li').each(function(G) {
          if ($(this).attr('lay-id') == D) {
            E = true;
            return false;
          }
        });
        if (!E) {
          if ((Cache.mTabList.length + 1) >= config.maxTabNum) {
            layer.msg('最多打开' + config.maxTabNum + '个选项卡', {
              icon: 2
            });
            Cache.go(Cache.mTabPosition);
            return;
          }
          element.tabAdd(PAGE_TABS, {
            id     : D,
            title  : '<span class="title">' + (w || '') + '</span>',
            content: '<div lay-id="' + D + '" lay-hash="' + B + '"></div>'
          });
          (B != NPPP) && Cache.mTabList.push(x);
          (config.cacheTab) && admin.putTempData('indexTabs', Cache.mTabList);
        }
        z = $tab + '>.layui-tab-content>.layui-tab-item>div[lay-id="' + D + '"]';
        var C = $(z).attr('lay-hash');
        if (B != C) {
          $(z).attr('lay-hash', B);
          E = false;
          for (var y = 0; y < Cache.mTabList.length; y++) {
            if (Cache.mTabList[y].menuId == C) {
              Cache.mTabList[y] = x;
            }
          }(config.cacheTab) && admin.putTempData('indexTabs', Cache.mTabList);
        }
        if (!E || layRouter.isRefresh) {
          Cache.renderView(v, z);
        }
        if (!x.noChange) {
          element.tabChange(PAGE_TABS, D);
        }
      } else {
        var F = $(z);
        if (!F || F.length <= 0) {
          var A = '<div class="layui-body-header">';
          A += '      <span class="layui-body-header-title"></span>';
          A += '      <span class="layui-breadcrumb pull-right">';
          A += '         <a href="#' + NPPP + '">首页</a>';
          A += '         <a><cite></cite></a>';
          A += '      </span>';
          A += '   </div>';
          A += '   <div lay-id="' + B + '"></div>';
          $($body).html(A);
          element.render('breadcrumb');
        } else {
          F.attr('lay-id', B);
        } if (NPPP != B) {
          Cache.setTabTitle(w);
        } else {
          Cache.setTabTitle(undefined);
        }
        admin.activeNav(B);
        Cache.mTabList.splice(0, Cache.mTabList.length);
        (NPPP != B) && Cache.mTabList.push(x);
        (config.cacheTab) && admin.putTempData('indexTabs', Cache.mTabList);
        Cache.mTabPosition = B;
        Cache.renderView(v, z);
      }(admin.getPageWidth() <= 768) && admin.flexible(true);
      $('.layui-table-tips-c').trigger('click');
    },
    renderView: function(w, x, y) {
      var v = $(x);
      !y && (y = v.parent());
      admin.showLoading({
        elem: y,
        size: ''
      });
      v.load(w, function() {
        admin.renderPerm();
        setTimeout(function() {
          admin.removeLoading(y);
        }, 150);
      });
    },
    loadHome: function(y) {
      var A = y.url;
      var v = y.name;
      var z = admin.getTempData('indexTabs');
      NPPP = A.substring(1);
      Cache.regRouter([y]);
      if (config.pageTabs) {
        var w = Cache.getHashPath(A);
        Cache.loadView({
          menuId  : NPPP,
          menuPath: config.viewPath + w + Cache.getViewSuffix(w),
          menuName: v,
          noChange: true
        });
      }
      var x = (y.loadSetting == undefined ? true : y.loadSetting);
      if (x) {
        Cache.loadSettings(z);
      }
      layRouter.init({
        index   : NPPP,
        notFound: function(B) {
          config.routerNotFound && config.routerNotFound(B);
        }
      });
    },
    openNewTab: function(v) {
      Cache.regRouter([v]);
      Cache.go(v.url.substring(1));
    },
    closeTab: function(w) {
      var v = Cache.getHashPath('#' + w);
      element.tabDelete(PAGE_TABS, v);
    },
    go: function(v) {
      layRouter.go(v);
    },
    getHashPath: function(y) {
      var v = layui.router(y);
      var x = '';
      for (var w = 0; w < v.path.length; w++) {
        x += ('/' + v.path[w]);
      }
      return x;
    },
    setTabTitle: function(w, v) {
      if (!config.pageTabs) {
        if (w) {
          $($bodyHeader).addClass('show');
          var x = $($bodyHeader + '>.layui-body-header-title');
          x.html(w);
          x.next('.layui-breadcrumb').find('cite').last().html(w);
        } else {
          $($bodyHeader).removeClass('show');
        }
      } else {
        w || (w = '');
        v || (v = Cache.getHashPath());
        v && $($tab + '>.layui-tab-title>li[lay-id="' + v + '"] .title').html(w);
      }
    },
    setTabTitleHtml: function(v) {
      if (!config.pageTabs) {
        if (v) {
          $($bodyHeader).addClass('show');
          $($bodyHeader).html(v);
        } else {
          $($bodyHeader).removeClass('show');
        }
      }
    },
    getViewSuffix: function(w) {
      var v;
      if (typeof config.viewSuffix === 'string') {
        v = config.viewSuffix;
      } else {
        v = config.viewSuffix(w);
      } if (config.version != undefined) {
        if (v.indexOf('?') == -1) {
          v += '?v=';
        } else {
          v += '&v=';
        } if (config.version == true) {
          v += new Date().getTime();
        } else {
          v += config.version;
        }
      }
      return v;
    },
    loadSettings: function(y) {
      if (config.cacheTab) {
        var z = y;
        if (z) {
          for (var w = 0; w < z.length; w++) {
            if (config.pageTabs) {
              z[w].noChange = true;
              Cache.loadView(z[w]);
            }
          }
        }
      }
      var v = layui.data(config.tableName);
      if (v) {
        var x = v.openFooter;
        if (x != undefined && x == false) {
          $('body.layui-layout-body').addClass('close-footer');
        }
        if (v.navArrow != undefined) {
          $($sideScroll + '>.layui-nav-tree').removeClass('arrow2 arrow3');
          v.navArrow && $($sideScroll + '>.layui-nav-tree').addClass(v.navArrow);
        }
      }
    }
  };
  var j = layui.data(config.tableName);
  if (j) {
    var p = j.openTab;
    if (p != undefined) {
      config.pageTabs = p;
    }
  }
  var f = '.layui-layout-admin .site-mobile-shade';
  if ($(f).length <= 0) {
    $('.layui-layout-admin').append('<div class="site-mobile-shade"></div>');
  }
  $(f).click(function() {
    admin.flexible(true);
  });
  if (config.pageTabs && $($tab).length <= 0) {
    var e = '<div class="layui-tab" lay-allowClose="true" lay-filter="admin-pagetabs">';
    e += '       <ul class="layui-tab-title"></ul>';
    e += '      <div class="layui-tab-content"></div>';
    e += '   </div>';
    e += '   <div class="layui-icon admin-tabs-control layui-icon-prev" ew-event="leftPage"></div>';
    e += '   <div class="layui-icon admin-tabs-control layui-icon-next" ew-event="rightPage"></div>';
    e += '   <div class="layui-icon admin-tabs-control layui-icon-down">';
    e += '      <ul class="layui-nav admin-tabs-select" lay-filter="admin-pagetabs-nav">';
    e += '         <li class="layui-nav-item" lay-unselect>';
    e += '            <a></a>';
    e += '            <dl class="layui-nav-child layui-anim-fadein">';
    e += '               <dd ew-event="closeThisTabs" lay-unselect><a>关闭当前标签页</a></dd>';
    e += '               <dd ew-event="closeOtherTabs" lay-unselect><a>关闭其它标签页</a></dd>';
    e += '               <dd ew-event="closeAllTabs" lay-unselect><a>关闭全部标签页</a></dd>';
    e += '            </dl>';
    e += '         </li>';
    e += '      </ul>';
    e += '   </div>';
    $($body).html(e);
    element.render('nav');
  }
  element.on('tab(' + PAGE_TABS + ')', function(w) {
    var v = $(this).attr('lay-id');
    var x = $($tab + '>.layui-tab-content>.layui-tab-item>div[lay-id="' + v + '"]').attr('lay-hash');
    Cache.mTabPosition = x;
    admin.activeNav(x);
    Cache.go(x);
    admin.resizeTable(0);
    admin.rollPage('auto');
  });
  element.on('tabDelete(' + PAGE_TABS + ')', function(w) {
    var v = Cache.mTabList[w.index - 1];
    if (v) {
      Cache.mTabList.splice(w.index - 1, 1);
      (config.cacheTab) && admin.putTempData('indexTabs', Cache.mTabList);
    }
    if ($($tab + '>.layui-tab-title>li.layui-this').length <= 0) {
      $($tab + '>.layui-tab-title>li:last').trigger('click');
    }
  });
  $('body').off('click.navMore').on('click.navMore', '[nav-bind]', function() {
    var v = $(this).attr('nav-bind');
    $('ul[lay-filter="' + SIDE_NAV + '"]').addClass('layui-hide');
    $('ul[nav-id="' + v + '"]').removeClass('layui-hide');
    if (admin.getPageWidth() <= 768) {
      admin.flexible(false);
    }
    $($header + '>.layui-nav .layui-nav-item').removeClass('layui-this');
    $(this).parent('.layui-nav-item').addClass('layui-this');
  });
  if (config.openTabCtxMenu && config.pageTabs) {
    $($tab + '>.layui-tab-title').off('contextmenu.tab').on('contextmenu.tab', 'li', function(w) {
      var v = $(this).attr('lay-id');
      contextMenu.show([{
        icon : 'layui-icon layui-icon-refresh',
        name : '刷新当前',
        click: function() {
          element.tabChange(PAGE_TABS, v);
          admin.refresh(Cache.getHashPath('#' + v));
        }
      }, {
        icon : 'layui-icon layui-icon-close-fill ctx-ic-lg',
        name : '关闭当前',
        click: function() {
          admin.closeThisTabs(v);
        }
      }, {
        icon : 'layui-icon layui-icon-unlink',
        name : '关闭其他',
        click: function() {
          admin.closeOtherTabs(v);
        }
      }, {
        icon : 'layui-icon layui-icon-close ctx-ic-lg',
        name : '关闭全部',
        click: function() {
          admin.closeAllTabs();
        }
      }], w.clientX, w.clientY);
      return false;
    });
  }
  t('index', Cache);
});
